package com.atguigu.guli.service.edu.controller.api;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-11-24
 */
@RestController
@RequestMapping("/api/edu/teacher")  //管理员要考虑CRUD的权限管理、用户要考虑登录状态的验证+限流+黑名单白名单
public class ApiTeacherController {
    @Autowired
    TeacherService teacherService;
    //查询所有讲师
//    @ResponseBody
    @GetMapping("list")  //restController中的接口返回值会被转为json
    public R queryAll(){
        try {
            int i = 1/0;
        } catch (Exception e) {
            return R.setResult(ResultCodeEnum.DIV_ZERO_ERROR);
        }
        // r = {code:xxx , message:xxx , success:true , data:  {items: [{},{}] } }
        //  r.data.items
        return R.ok().data("items",teacherService.list());
    }
    //新增讲师
    @PostMapping("save")
//    public String save(Teacher teacher){ //POJO入参  url?name=xx&age=xxx&xxx
    public R save(@RequestBody Teacher teacher){ //获取请求体参数： 以json的方式存到请求体中
        if(teacherService.save(teacher)){
            return R.ok();
        }
        return R.error();
    }
}

